/****************************************************************************
 * Copyright (C) 2010
 * by Dimok
 * modified by dj_skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * AppControls.cpp
 *
 * for SaveGame Manager GX 2012
 ***************************************************************************/
#include <ogcsys.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wiiuse/wpad.h>
#include <ogc/pad.h>

#include "AppControls.hpp"
#include "../FileOperations/fileops.h"


AppControls::AppControls()
{
	SetDefault();
}

AppControls::~AppControls()
{
}

void AppControls::SetDefault()
{
	//ScreenshotHoldButton = WPAD_NUNCHUK_BUTTON_Z >> 16;
	//ScreenshotClickButton = WPAD_NUNCHUK_BUTTON_C >> 16;

	WiiControls.ClickButton = WPAD_BUTTON_A;
	WiiControls.BackButton = WPAD_BUTTON_B;
	WiiControls.UpButton = WPAD_BUTTON_UP;
	WiiControls.DownButton = WPAD_BUTTON_DOWN;
	WiiControls.LeftButton = WPAD_BUTTON_LEFT;
	WiiControls.RightButton = WPAD_BUTTON_RIGHT;
	WiiControls.NextButton = WPAD_BUTTON_PLUS;
	WiiControls.PrevButton = WPAD_BUTTON_MINUS;
	WiiControls.HomeButton = WPAD_BUTTON_HOME;
	WiiControls.UpInDirectory = WPAD_BUTTON_B;
	
	ClassicControls.ClickButton = WPAD_CLASSIC_BUTTON_A >> 16;
	ClassicControls.BackButton = WPAD_CLASSIC_BUTTON_B >> 16;
	ClassicControls.UpButton = WPAD_CLASSIC_BUTTON_UP >> 16;
	ClassicControls.DownButton = WPAD_CLASSIC_BUTTON_DOWN >> 16;
	ClassicControls.LeftButton = WPAD_CLASSIC_BUTTON_LEFT >> 16;
	ClassicControls.RightButton = WPAD_CLASSIC_BUTTON_RIGHT >> 16;
	ClassicControls.NextButton = WPAD_CLASSIC_BUTTON_PLUS >> 16;
	ClassicControls.PrevButton = WPAD_CLASSIC_BUTTON_MINUS >> 16;
	ClassicControls.HomeButton = WPAD_CLASSIC_BUTTON_HOME >> 16;
	ClassicControls.UpInDirectory = WPAD_CLASSIC_BUTTON_B >> 16;
	
	GCControls.ClickButton = PAD_BUTTON_A;
	GCControls.BackButton = PAD_BUTTON_B;
	GCControls.UpButton = PAD_BUTTON_UP;
	GCControls.DownButton = PAD_BUTTON_DOWN;
	GCControls.LeftButton = PAD_BUTTON_LEFT;
	GCControls.RightButton = PAD_BUTTON_RIGHT;
	GCControls.NextButton = PAD_TRIGGER_R;
	GCControls.PrevButton = PAD_TRIGGER_L;
	GCControls.HomeButton = PAD_BUTTON_MENU;
	GCControls.UpInDirectory = PAD_BUTTON_B;
}

bool AppControls::Load(std::string filepath)
{
	if(filepath.empty())
		return false;
	
	char line[1024];
	FilePath = filepath;
	FilePath += "/SaveGameGX_Controls.cfg";
	
	if(!CheckFile(FilePath))
		return false;
	
	FILE * file = fopen(FilePath.c_str(), "r");
	if (!file)
	{
		fclose(file);
		return false;
	}
	
	while (fgets(line, sizeof(line), file))
	{
		if (line[0] == '#') continue;
		
		this->ParseLine(line);
	}
	fclose(file);
	
	return true;
}

bool AppControls::Save()
{
	char destpath[1024];
	snprintf(destpath, sizeof(destpath), "%s", FilePath.c_str());
	
	char * ptr = strrchr(destpath, '/');
	if(ptr)
	{
		ptr++;
		ptr[0] = '\0';
	}
	
	if(!CreateSubfolder(destpath))
		return false;
	
	FILE * file = fopen(FilePath.c_str(), "w");
	if(!file)
	{
		fclose(file);
		return false;
	}
	
	fprintf(file, "# SaveGame Manager GX Controls\n");
	fprintf(file, "# Note: This file is automatically generated\n\n");
	fprintf(file, "# Wii Controls\n\n");
	fprintf(file, "WiiControls.ClickButton = %d\n", WiiControls.ClickButton);
	fprintf(file, "WiiControls.BackButton = %d\n", WiiControls.BackButton);
	fprintf(file, "WiiControls.UpButton = %d\n", WiiControls.UpButton);
	fprintf(file, "WiiControls.DownButton = %d\n", WiiControls.DownButton);
	fprintf(file, "WiiControls.LeftButton = %d\n", WiiControls.LeftButton);
	fprintf(file, "WiiControls.RightButton = %d\n", WiiControls.RightButton);
	fprintf(file, "WiiControls.NextButton = %d\n", WiiControls.NextButton);
	fprintf(file, "WiiControls.PrevButton = %d\n", WiiControls.PrevButton);
	fprintf(file, "WiiControls.HomeButton = %d\n", WiiControls.HomeButton);
	fprintf(file, "WiiControls.UpInDirectory = %d\n", WiiControls.UpInDirectory);

	fprintf(file, "\n# Wii Classic Controls\n\n");
	fprintf(file, "ClassicControls.ClickButton = %d\n", ClassicControls.ClickButton);
	fprintf(file, "ClassicControls.BackButton = %d\n", ClassicControls.BackButton);
	fprintf(file, "ClassicControls.UpButton = %d\n", ClassicControls.UpButton);
	fprintf(file, "ClassicControls.DownButton = %d\n", ClassicControls.DownButton);
	fprintf(file, "ClassicControls.LeftButton = %d\n", ClassicControls.LeftButton);
	fprintf(file, "ClassicControls.RightButton = %d\n", ClassicControls.RightButton);
	fprintf(file, "ClassicControls.NextButton = %d\n", ClassicControls.NextButton);
	fprintf(file, "ClassicControls.PrevButton = %d\n", ClassicControls.PrevButton);
	fprintf(file, "ClassicControls.HomeButton = %d\n", ClassicControls.HomeButton);
	fprintf(file, "ClassicControls.UpInDirectory = %d\n", ClassicControls.UpInDirectory);

	fprintf(file, "\n# GC Controls\n\n");
	fprintf(file, "GCControls.ClickButton = %d\n", GCControls.ClickButton);
	fprintf(file, "GCControls.BackButton = %d\n", GCControls.BackButton);
	fprintf(file, "GCControls.UpButton = %d\n", GCControls.UpButton);
	fprintf(file, "GCControls.DownButton = %d\n", GCControls.DownButton);
	fprintf(file, "GCControls.LeftButton = %d\n", GCControls.LeftButton);
	fprintf(file, "GCControls.RightButton = %d\n", GCControls.RightButton);
	fprintf(file, "GCControls.NextButton = %d\n", GCControls.NextButton);
	fprintf(file, "GCControls.PrevButton = %d\n", GCControls.PrevButton);
	fprintf(file, "GCControls.HomeButton = %d\n", GCControls.HomeButton);
	fprintf(file, "GCControls.UpInDirectory = %d\n", GCControls.UpInDirectory);

	/*fprintf(file, "\n# Screenshot combination buttons\n\n");
	fprintf(file, "ScreenshotHoldButton = %d\n", ScreenshotHoldButton);
	fprintf(file, "ScreenshotClickButton = %d\n", ScreenshotClickButton);
	*/
	
	fclose(file);

	return true;
}

bool AppControls::SetControl(char *name, char *value)
{
	int i = 0;

	/*if (strcmp(name, "ScreenshotHoldButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			ScreenshotHoldButton = i;
		}
		return true;
	}
	else if (strcmp(name, "ScreenshotClickButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			ScreenshotClickButton = i;
		}
		return true;
	}
	else */if (strcmp(name, "WiiControls.ClickButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			WiiControls.ClickButton = i;
		}
		return true;
	}
	else if (strcmp(name, "WiiControls.BackButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			WiiControls.BackButton = i;
		}
		return true;
	}
	else if (strcmp(name, "WiiControls.UpButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			WiiControls.UpButton = i;
		}
		return true;
	}
	else if (strcmp(name, "WiiControls.DownButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			WiiControls.DownButton = i;
		}
		return true;
	}
	else if (strcmp(name, "WiiControls.LeftButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			WiiControls.LeftButton = i;
		}
		return true;
	}
	else if (strcmp(name, "WiiControls.RightButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			WiiControls.RightButton = i;
		}
		return true;
	}
	else if (strcmp(name, "WiiControls.NextButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			WiiControls.NextButton = i;
		}
		return true;
	}
	else if (strcmp(name, "WiiControls.PrevButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			WiiControls.PrevButton = i;
		}
		return true;
	}
	else if (strcmp(name, "WiiControls.HomeButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			WiiControls.HomeButton = i;
		}
		return true;
	}
	else if (strcmp(name, "WiiControls.UpInDirectory") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			WiiControls.UpInDirectory = i;
		}
		return true;
	}
	else if (strcmp(name, "ClassicControls.ClickButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			ClassicControls.ClickButton = i;
		}
		return true;
	}
	else if (strcmp(name, "ClassicControls.BackButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			ClassicControls.BackButton = i;
		}
		return true;
	}
	else if (strcmp(name, "ClassicControls.UpButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			ClassicControls.UpButton = i;
		}
		return true;
	}
	else if (strcmp(name, "ClassicControls.DownButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			ClassicControls.DownButton = i;
		}
		return true;
	}
	else if (strcmp(name, "ClassicControls.LeftButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			ClassicControls.LeftButton = i;
		}
		return true;
	}
	else if (strcmp(name, "ClassicControls.RightButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			ClassicControls.RightButton = i;
		}
		return true;
	}
	else if (strcmp(name, "ClassicControls.NextButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			ClassicControls.NextButton = i;
		}
		return true;
	}
	else if (strcmp(name, "ClassicControls.PrevButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			ClassicControls.PrevButton = i;
		}
		return true;
	}
	else if (strcmp(name, "ClassicControls.HomeButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			ClassicControls.HomeButton = i;
		}
		return true;
	}
	else if (strcmp(name, "ClassicControls.UpInDirectory") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			ClassicControls.UpInDirectory = i;
		}
		return true;
	}
	else if (strcmp(name, "GCControls.ClickButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			GCControls.ClickButton = i;
		}
		return true;
	}
	else if (strcmp(name, "GCControls.BackButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			GCControls.BackButton = i;
		}
		return true;
	}
	else if (strcmp(name, "GCControls.UpButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			GCControls.UpButton = i;
		}
		return true;
	}
	else if (strcmp(name, "GCControls.DownButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			GCControls.DownButton = i;
		}
		return true;
	}
	else if (strcmp(name, "GCControls.LeftButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			GCControls.LeftButton = i;
		}
		return true;
	}
	else if (strcmp(name, "GCControls.RightButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			GCControls.RightButton = i;
		}
		return true;
	}
	else if (strcmp(name, "GCControls.NextButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			GCControls.NextButton = i;
		}
		return true;
	}
	else if (strcmp(name, "GCControls.PrevButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			GCControls.PrevButton = i;
		}
		return true;
	}
	else if (strcmp(name, "GCControls.HomeButton") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			GCControls.HomeButton = i;
		}
		return true;
	}
	else if (strcmp(name, "GCControls.UpInDirectory") == 0) {
		if (sscanf(value, "%d", &i) == 1) {
			GCControls.UpInDirectory = i;
		}
		return true;
	}
	
	return false;
}

void AppControls::ParseLine(char *line)
{
	char temp[200], name[200], value[200];

	strncpy(temp, line, sizeof(temp));

	char * eq = strchr(temp, '=');

	if(!eq) return;

	*eq = 0;

	this->TrimLine(name, temp, sizeof(name));
	this->TrimLine(value, eq+1, sizeof(value));

	this->SetControl(name, value);
}

void AppControls::TrimLine(char *dest, char *src, int size)
{
	int len;
	while (*src == ' ') src++;
	len = strlen(src);
	while (len > 0 && strchr(" \r\n", src[len-1])) len--;
	if (len >= size) len = size-1;
	strncpy(dest, src, len);
	dest[len] = 0;
}
